﻿using ClearDir.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ClearDir
{

    /// <summary>
    /// 属性资源文件导出
    /// </summary>
    public class DependentFiles
    {
        /// <summary>
        /// 获取当前运行路径
        /// </summary>
        public static string curDir()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        /// <summary>
        /// 获取当前运行Exe的路径
        /// </summary>
        public static string curExecutablePath()
        {
            return System.Windows.Forms.Application.ExecutablePath;
        }

        /// <summary>
        /// 检测目录是否存在，若不存在则创建
        /// </summary>
        public static void checkDir(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }


        /// <summary>
        /// 保存Byte数组为文件
        /// </summary>
        public static void SaveFile(Byte[] array, string path, bool repalce = false)
        {
            if (repalce && System.IO.File.Exists(path)) System.IO.File.Delete(path);    // 若目标文件存在，则替换
            if (!System.IO.File.Exists(path))
            {
                // 创建输出路径
                String dir = System.IO.Path.GetDirectoryName(path);
                checkDir(dir);

                // 创建输出流
                System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create);

                //将byte数组写入文件中
                fs.Write(array, 0, array.Length);
                fs.Close();
            }
        }

        /// <summary>
        /// 生成Excuter
        /// </summary>
        public static String getExcuterPath()
        {
            String path = curDir() + "Excuter.exe";
            SaveFile(Resources.Excuter, path, true);
            return path;
        }

        /// <summary>
        /// 生成Code
        /// </summary>
        public static string getCodeFile()
        {
            String path = curDir() + "Code.txt";
            String Code = Encoder.Encode(Resources.ClearTool);
            byte[] B = Encoding.UTF8.GetBytes(Code);

            SaveFile(B, path, true);
            return path;
        }

        /// <summary>
        /// 删除当前应用程序
        /// </summary>
        public static void deletCurExe()
        {
            // 通过Excuter进行删除, 删除sourceCode和当前程序
            try
            {
                String curExe = curExecutablePath();
                String Excuter = getExcuterPath();
                String sourceCode = getCodeFile();

                //System.Diagnostics.Process.Start(Excuter, "\"" + sourceCode + "\"" + " " + "\"" + sourceCode + "\"" + " " + "\"" + curExe + "\"" + " DEBUG");
                //System.Diagnostics.Process.Start(Excuter, "\"" + sourceCode + "\"" + " " + "\"" + sourceCode + "\"" + " " + "\"" + curExe + "\"" + " DELAY2000");
                System.Diagnostics.Process.Start(Excuter, "\"" + sourceCode + "\"" + " " + "\"" + sourceCode + "\"" + " " + "\"" + curExe + "\"" + " AUTODELET");
            }
            catch (Exception ex) { }

            // 通过vbs进行删除
            //AutoDelet.deletItself();
        }


        /// <summary>
        /// 获取文件中的数据,自动判定编码格式
        /// </summary>
        private static string fileToString(String filePath)
        {
            string str = "";

            //获取文件内容
            if (File.Exists(filePath))
            {
                StreamReader file1;

                file1 = new StreamReader(filePath, Encoding.UTF8);  // 读取文件中的数据
                str = file1.ReadToEnd();                            // 读取文件中的全部数据

                file1.Close();
                file1.Dispose();
            }
            return str;
        }

    }

}
